package com.dingpeikeji.quickdeer.modules.wx.site.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 站点数据
 *
 */
@Controller
@RequestMapping("${wxPath}/data/site")
public class WxSiteController {

	@Autowired
	private TSitedtlService sitedtlService;
	
	@RequestMapping("siteJson")
	@ResponseBody
	public List<Map<String,Object>> siteJson(String spottypes) {
		TSitedtl query = new TSitedtl();
		if(!ValidateTool.isEmptyOrNull(spottypes)) {
			query.setSpottypes(spottypes.split(","));
		}
		List<TSitedtl> siteList = sitedtlService.findList(query);
		// 对每个数据集进行格式化
		
		String[] characters = new String[]{"热门","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
		List<Map<String,Object>> data = Lists.newArrayList();
		for(int i=0;i<characters.length;i++) {
			Map<String,Object> group = Maps.newHashMap();
			List<TSitedtl> list = Lists.newArrayList();
			group.put("character", characters[i]);
			group.put("list", list);
			for(TSitedtl site : siteList) {
				String c = site.getQuerycode().substring(0, 1);
				if(c.toUpperCase().equals(characters[i]) ||
						(characters[i]=="热门" && "1".equals(site.getIshot()))) {
					@SuppressWarnings("unchecked")
					List<TSitedtl> temp = (List<TSitedtl>) group.get("list");
					String queryCode = site.getQuerycode().replace(" ", "_");
					queryCode = StringUtils.toCapitalizeCamelCase(queryCode);
					site.setQuerycode(queryCode);
					//site.setSiteid(upCase(queryCode));
					temp.add(site);
				}
			}
			data.add(i, group);
		}
		return data;
	}

	/*private static String upCase(String s) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < s.length(); i++) {
			if (Character.isUpperCase(s.charAt(i))) {
				sb.append(s.charAt(i));
			}
		}
		return sb.toString();
	}*/
}
